﻿using System;
using System.Collections.Generic;
using System.Text;
using jade.core;
using MarketSimulator.Utils.Maps;
using MarketSimulator.Utils.Attributes;
using System.Windows.Forms;
using MarketSimulator.Agents.Behaviors;

namespace MarketSimulator.Agents.Agents
{
    /// <summary>
    /// Transporter Agent
    /// </summary>
    public class TransporterAgent : Agent
    {
        public static object TLock = new object();

        public static int TransporterAgentCount = 0;

        public Action<string> transportChat;

        public Action<string> actorsChat;

        private Form TheForm;

        public override void setup()
        {
            try
            {
                object[] args = getArguments();
                TheForm = args[0] as Form;
                transportChat = args[1] as Action<string>;
                actorsChat = args[2] as Action<string>;

                //Create Transporter info
                int transporterNo;
                Route route;
                long capacity;
                double price;

                lock (TLock)
                {
                    transporterNo = ++TransporterAgentCount;
                    route = WorldMap.Routes[(transporterNo - 1) % WorldMap.Routes.Count];
                    Random rand = new Random();
                    capacity = rand.Next(10, 100);
                    price = rand.NextDouble() * 100;
                }
                    TransporterAttributes transporterAttributes = new TransporterAttributes(this, route, capacity, price, WriteToAgentsChat);
                    transporterAttributes.RegisterAgent();
                
                TheForm.Invoke(transportChat, string.Format("{0} : Transport Info = {1}", this.getLocalName(), transporterAttributes.GetTransporterInfoString()));

                //Add Behavior Section
                addBehaviour(new TransporterBehavior(transporterAttributes));
            }
            catch (Exception ex)
            {
                TheForm.Invoke(transportChat, ex.Message);
            }
        }

        public void WriteToAgentsChat(string message)
        {
            TheForm.Invoke(actorsChat, message);
        }
    }
}
